package com.epfresh.activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.epfresh.adapter.*;
import com.epfresh.R;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.RequestEntityMap;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.api.utils.ToastUtils;
import com.epfresh.bean.Address;
import com.epfresh.bean.City;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseApplication;
import com.epfresh.global.DataManager;
import com.epfresh.location.PoiAddressActivity;
import com.epfresh.views.CommonInputItem;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.epfresh.bean.*;
import com.google.gson.reflect.TypeToken;

public class PerfectInfoActivity extends BaseActivity {

    static final String TAG = "PerfectInfoActivity";
    private TextView now_version;
    private ImageView apk_code;

    String[] hints = new String[]{"店招名称","单位名称及灶","市场名称+铺位号" ,"店招名称","店招名称及分店名","企业/公司名称或个人姓名"};

    CommonInputItem ciName;
    CommonInputItem ciPhone;
    CommonInputItem ciArea;
    CommonInputItem ciDetailAddress;
    CommonInputItem ciCode;
    Address address;
    private boolean isLimitArea;
    private String addressStr;
    private boolean isSavingAddress;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfect_info);
        initView();
//        httpLoad();
        initPopup();
    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("完善信息");
        rightbtn.setText("保存");
        rightbtn.setVisibility(View.VISIBLE);
        rightbtn.setOnClickListener(this);
        baseback.setVisibility(View.GONE);
        ciName = (CommonInputItem) findViewById(R.id.ci_name);
        ciPhone = (CommonInputItem) findViewById(R.id.ci_phone);
        ciArea = (CommonInputItem) findViewById(R.id.ci_area);
        ciDetailAddress = (CommonInputItem) findViewById(R.id.ci_detail_address);
        ciCode = (CommonInputItem) findViewById(R.id.ci_code);
        ciArea.setOnClickListener(this);


        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_1Background = (GradientDrawable)tv_1.getBackground();
        tv_2Background = (GradientDrawable)tv_2.getBackground();

        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        adapter = new GridBodyAdapter(this,10);
        adapter.setOnItemClickListener(this);
        GridView gridView = (GridView) findViewById(R.id.gv_body);
        gridView.setAdapter(adapter);
    }
    TextView tv_1,tv_2;
    GradientDrawable tv_1Background,tv_2Background;
    int preferenceFlag = 3;//0，全部；1，清真；
    GridBodyAdapter adapter;
    public void onClick(View view) {
        if(view.getTag(R.id.item_key_status) instanceof Integer){
            int flagPosition = (int)view.getTag(R.id.item_key_status);
            adapter.flagPosition = flagPosition;
            adapter.notifyDataSetChanged();
            ciName.setContentHintText(hints[flagPosition]);
            return;
        }
        switch (view.getId()) {
            case R.id.rightbtn:
                if(adapter.flagPosition > 6) {
                    toast("请选择主体类型");
                    return;
                }

                submit();
                break;


            case R.id.ci_area:
//                if (cityData == null) {
//                    if (isLimitArea) {
//                        requestArea();
//                    } else {
//                        requestLocalCity();
//                    }
//                } else {
//                    intoPoiAddress();
//                }
                showPopup();
                break;
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.find_new:
                // 返回上一页
                ToastUtils.TextToast(mContext, BaseApplication.getDoc().getLatestVersion());
                break;
            case R.id.tv_1:
                initPreference(1);
                break;
            case R.id.tv_2:
                initPreference(0);
            default:
                break;
        }
    }

    void initPreference(int i){
        preferenceFlag = i;
        int colorGrey = Color.parseColor("#999999");
        int colorGreen = Color.parseColor("#3eb049");
        tv_1.setTextColor(colorGrey);
        tv_1Background.setStroke(1,colorGrey);
        tv_2.setTextColor(colorGrey);
        tv_2Background.setStroke(1,colorGrey);
        if(i==0){
            tv_2.setTextColor(colorGreen);
            tv_2Background.setStroke(1,colorGreen);
        }else {
            tv_1.setTextColor(colorGreen);
            tv_1Background.setStroke(1,colorGreen);
        }
    }

    private OnRequestListener<City> onRequestAreaListener = new OnRequestListener<City>() {
        @Override
        public City jsonToObj(String s) {
            return new Gson().fromJson(s, City.class);
        }

        @Override
        public void onFail(int i, Object o, Object o1) {
            isSavingAddress = false;
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int i, Object o, Object o1) {
            isSavingAddress = false;
            hideProgressDialog();
        }

        @Override
        public void onStart(Object o) {

            isSavingAddress = true;
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<City> responseEntity, Object o) {
            isSavingAddress = false;
            hideProgressDialog();
            City responseElement = responseEntity.getResponseElement();
            if (responseElement != null && responseElement.getAddressList() != null && responseElement.getAddressList().size() > 0) {
                cityData = responseElement;
                intoPoiAddress();
            }

        }
    };

    public void requestArea() {
        RequestEntityMap re = new RequestEntityMap();
        re.setCmd(AppPurchaseRouterConstant.cmd_account_app_deliveryAddress);

        request(re, AppPurchaseRouterConstant.cmd_account_app_deliveryAddress, onRequestAreaListener);
    }



    public void requestLocalCity() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected Void doInBackground(Void... voids) {
                cityData = DataManager.getInstance().getCityData(PerfectInfoActivity.this);

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                intoPoiAddress();
            }
        }.execute();
    }

    City cityData;



    private void intoPoiAddress() {
        Intent intent = new Intent(this, PoiAddressActivity.class);

//        isLimitArea = true;
        if (address != null && address.getLat() != null && !"".equals(address.getLat())){
            intent.putExtra("cityName",address.getCityName());
            intent.putExtra("lat",address.getLat());
            intent.putExtra("lng",address.getLng());
        }
        if (isLimitArea) {
            intent.putExtra("isLimit", true);
        }
        if (cityData != null)
            intent.putExtra("cityData", new Gson().toJson(cityData));
        startActivityForResult(intent, 0x1992);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == 0x1992) {
            if (data != null) {
                String detailAddress = data.getStringExtra("detail_address");
                String addressStr = data.getStringExtra("address");
                String poiAddress = data.getStringExtra("poi_address");

                String latitude = data.getStringExtra("latitude");
                String longitude = data.getStringExtra("longitude");

                String provinceName = data.getStringExtra("provinceName");
                String cityName = data.getStringExtra("cityName");
                String districtName = data.getStringExtra("DistrictName");
                String provinceId = data.getStringExtra("provinceId");
                String cityId = data.getStringExtra("cityId");
                String districtId = data.getStringExtra("DistrictId");

                if(address == null){
                    this.address = new Address();
                }


                address.setProvinceName(provinceName);
                address.setCityName(cityName);
                address.setDistrictName(districtName);

                address.setProvinceId(provinceId);
                address.setCityId(cityId);
                address.setDistrictId(districtId);

                address.setLat(latitude);
                address.setLng(longitude);

                address.setAddressOnMap(detailAddress);
                address.setAddress(addressStr);
                ciArea.setContentText(detailAddress);
                ciDetailAddress.setContentText(addressStr);
            }
        }
    }

    /**
     * 提交完善信息
     */
    private void submit() {
        String nick = ciName.getContentText();
        if(nick==null||"".equals(nick)) {
            toast("请输入"+hints[adapter.flagPosition]);
            return;
        }
        String name = ciPhone.getContentText();
        if(name==null||"".equals(name)) {
            toast("请输入收货人");
            return;
        }
        if(ciArea.getContentText()==null||ciDetailAddress.getContentText()==null||"".equals(ciArea.getContentText())||"".equals(ciDetailAddress.getContentText())) {
            toast("请输入地址");
            return;
        }
        if(preferenceFlag == 3) {
            toast("请选择偏好设置");
            return;
        }
        final RequestEntityMap requestEntity = new RequestEntityMap();
        requestEntity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_account_acc_completeInformation);
        Map<String, Object> map = new HashMap<>();
        map.put("subjectType", ""+adapter.flagPosition );//主体类型（0：餐饮饭店 1：机关食堂 2.菜市场 3.社区生鲜 4.超市 5.其他）
        map.put("nickname", ciName.getContentText());
        map.put("preference", ""+preferenceFlag);//偏好设置（0：查看全部商品 1：只查看清真商品）
        map.put("id", getUser().getAccountId());
        //Log.e("json___",)
        if(address==null)
            address = new Address();
        address.setAddress(ciDetailAddress.getContentText());
        //address.setAddressOnMap(ciArea.getContentText());
        address.setName(ciPhone.getContentText());
        setAddressData();
        map.put("deliveryAddressModel", address);
        requestEntity.setParameters(map);
        request(requestEntity, com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_account_acc_completeInformation, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                toast("服务器出现小故障，请稍后重试");
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                toast("服务器出现小故障，请稍后重试");
            }

            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                hideProgressDialog();
                StatusEntity responseElement = response.getResponseElement();
                if (responseElement.isSuccess()) {
                    Intent main = new Intent(getApplicationContext(), HomeActivity.class);
                    main.putExtra("refreshFlag","refreshHomeFrag");
                    startActivity(main);
                    finish();
                }else {
                    toast("服务器出现小故障，请稍后重试");
                }

            }
        });
    }

    /**
     * 请求已有信息
     */
    private void httpLoad() {
        final RequestEntityMap requestEntity = new RequestEntityMap();
        requestEntity.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_account_acc_allInformation);
        request(requestEntity, com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_account_acc_allInformation, new OnRequestListener<InitInfoBean>() {
            @Override
            public InitInfoBean jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, InitInfoBean.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                toast("服务器出现小故障，请稍后重试");
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                toast("服务器出现小故障，请稍后重试");
            }

            @Override
            public void onStart(Object tag) {
            }

            @Override
            public void onResponse(ResponseEntity<InitInfoBean> response, Object tag) {
                InitInfoBean infoBean = response.getResponseElement();
                if(infoBean==null||infoBean.getResult()==null){
                    return;
                }
                InitInfoBean.ResultBean bean = infoBean.getResult();
                if(bean.getSubjectType()!=null){
                    adapter.flagPosition = bean.getSubjectType();
                    ciName.setContentHintText(hints[adapter.flagPosition]);
                    adapter.notifyDataSetChanged();
                }
                if(bean.getPreference()!=null){
                    initPreference(bean.getPreference());
                }
                if(bean.getNickname()!=null)
                    ciName.setContentText(bean.getNickname());
                if(bean.getDeliveryAddress()!=null&&bean.getDeliveryAddress().size()>0){
                    address = bean.getDeliveryAddress().get(0);
                    ciPhone.setContentText(address.getName());
                    ciArea.setContentText(address.getAddressOnMap());
                    ciDetailAddress.setContentText(address.getAddress());
                }


            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            Log.e("just","try back");
            finish();
        }
        return false;
    }

    //****************************************************************************************************************************************************
    private PopupWindow popupAddress;
    private PopupAddressAdapter addressAdapter;
    //顶部关闭按钮
    private ImageView img_close;
    //顶部的tab
    private TabLayout tab_layout;
    //地址列表
    private ListView lv_address;

    //被选中的省份name
    private String selectProvince = "";
    //被选中的城市name
    private String selectCity = "";
    //被选中的县或区域name
    private String selectArea = "";
    //被选中的街道name
    private String selectStreet = "";

    //被选中的省份code
    private String selectProvinceCode = "";
    //被选中的城市code
    private String selectCityCode = "";
    //被选中的县或区域code
    private String selectAreaCode = "";
    //被选中的街道code
    private String selectStreetCode = "";
    //被选中的街道的经纬度
    private String selectStreetLng;
    private String selectStreetLat;

    //省份level
    private String provinceLevel = "";
    //城市level
    private String cityLevel = "1";
    //县或区域level
    private String areaLevel = "2";
    //街道level
    private String streetLevel = "3";

    //被选中的省份在列表中的位置，默认没有选中的为-1
    private int provinceIndex = -1;
    //被选中的城市在列表中的位置，默认没有选中的为-1
    private int cityIndex = -1;
    //被选中的县或区在列表中的位置，默认没有选中的为-1
    private int areaIndex = -1;
    //被选中的街道在列表中的位置，默认没有选中的为-1
    private int streetIndex = -1;

    //省份的数据集合
    private List<PopupAddressEntity> provinces = new ArrayList<>();
    //城市的数据集合
    private List<PopupAddressEntity> cities = new ArrayList<>();
    //县和区域的数据集合
    private List<PopupAddressEntity> areas = new ArrayList<>();
    //街道的数据集合
    private List<PopupAddressEntity> streets = new ArrayList<>();

    //当前被选中的tab的index
    private int tabIndex;


    /**
     * 初始化popupWindow
     */
    private void initPopup() {
        View contentView = View.inflate(this, R.layout.layout_address_choose, null);
        img_close = contentView.findViewById(R.id.img_close);
        tab_layout = contentView.findViewById(R.id.tab_layout);
        lv_address = contentView.findViewById(R.id.lv_address);

        popupAddress = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupAddress.setBackgroundDrawable(new ColorDrawable());
        popupAddress.setOutsideTouchable(true);
        popupAddress.setAnimationStyle(R.style.ActionPopupAnimation);
        popupAddress.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupAddress.dismiss();
            }
        });

        tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateAddressLocalList(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        lv_address.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                updateItemClick(position);
            }
        });

        addressAdapter = new PopupAddressAdapter();
        lv_address.setAdapter(addressAdapter);

        reqAddressData("", 1, provinceLevel);
    }

    private void showPopup() {
        View rootView = View.inflate(this, R.layout.activity_add_address, null);
        popupAddress.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
    }


    /**
     * 根据item点击切换列表展示的地址数据
     *
     * @param position
     */
    private void updateItemClick(int position) {
        switch (tabIndex) {
            case 0:
                selectProvince = provinces.get(position).getName();
                selectProvinceCode = provinces.get(position).getCode();

                provinceIndex = position;
                addressAdapter.setSelectedPos(provinceIndex);
                reqAddressData(selectProvinceCode, 2, cityLevel);
                break;
            case 1:
                selectCity = cities.get(position).getName();
                selectCityCode = cities.get(position).getCode();

                cityIndex = position;
                addressAdapter.setSelectedPos(cityIndex);
                reqAddressData(selectCityCode, 3, areaLevel);
                break;
            case 2:
                selectArea = areas.get(position).getName();
                selectAreaCode = areas.get(position).getCode();

                areaIndex = position;
                addressAdapter.setSelectedPos(areaIndex);
                reqAddressData(selectAreaCode, 4, streetLevel);
                break;
            case 3:
                selectStreet = streets.get(position).getName();
                selectStreetCode = streets.get(position).getCode();
                selectStreetLng = streets.get(position).getLng();
                selectStreetLat = streets.get(position).getLat();

                streetIndex = position;
                addressAdapter.setSelectedPos(streetIndex);

                tab_layout.removeTabAt(3);
                //街道tab
                TabLayout.Tab tabStreet = tab_layout.newTab().setText(selectStreet);
                tab_layout.addTab(tabStreet);
                tabStreet.select();
                setTabTextSingle(tab_layout);

                ciArea.setContentText(selectProvince + " " + selectCity + " " + selectArea + " " + selectStreet);
                popupAddress.dismiss();
                break;
        }
    }

    private void setAddressData() {
        if (address != null) {
            address.setProvinceId(selectProvinceCode);
            address.setCityId(selectCityCode);
            address.setDistrictId(selectAreaCode);
            address.setAddressFourId(selectStreetCode);
            address.setAddressFour(selectStreet);
            address.setLng(selectStreetLng);
            address.setLat(selectStreetLat);
        }
    }


    /**
     * 更新列表缓存数据
     *
     * @param pos 当前选中tab的index
     */
    private void updateAddressLocalList(int pos) {
        switch (pos) {
            case 0:
                tabIndex = 0;
                addressAdapter.setData(provinces);
                addressAdapter.setSelectedPos(provinceIndex);
                lv_address.setSelection(provinceIndex == -1 ? 0 : provinceIndex);
                break;
            case 1:
                tabIndex = 1;
                addressAdapter.setData(cities);
                addressAdapter.setSelectedPos(cityIndex);
                lv_address.setSelection(cityIndex == -1 ? 0 : cityIndex);
                break;
            case 2:
                tabIndex = 2;
                addressAdapter.setData(areas);
                addressAdapter.setSelectedPos(areaIndex);
                lv_address.setSelection(areaIndex == -1 ? 0 : areaIndex);
                break;
            case 3:
                tabIndex = 3;
                addressAdapter.setData(streets);
                addressAdapter.setSelectedPos(streetIndex);
                lv_address.setSelection(streetIndex == -1 ? 0 : streetIndex);
                break;
        }
    }

    /**
     * 更新列表数据及tab状态
     *
     * @param list
     * @param addressList
     */
    private void updateAddressList(int list, List<PopupAddressEntity> addressList) {
        //省的tab
        TabLayout.Tab tabProvince;
        //城市的tab
        TabLayout.Tab tabCity;
        //区的tab
        TabLayout.Tab tabArea;
        //街道的tab
        TabLayout.Tab tabStreet;
        if (list == 1) {//更新一级（即省）列表
            //省
            tabProvince = tab_layout.newTab().setText("请选择");
            tab_layout.addTab(tabProvince);

            tabProvince.select();
            tabIndex = 0;

            provinces.addAll(addressList);
            addressAdapter.setData(provinces);
        } else if (list == 2) {//更新二级（即市）列表
            tab_layout.removeAllTabs();
            //省
            tabProvince = tab_layout.newTab().setText(selectProvince);
            tab_layout.addTab(tabProvince);
            //市
            tabCity = tab_layout.newTab().setText("请选择");
            tab_layout.addTab(tabCity);

            tabCity.select();
            tabIndex = 1;

            cityIndex = -1;
            addressAdapter.setSelectedPos(cityIndex);
            cities.clear();
            cities.addAll(addressList);
            addressAdapter.setData(cities);
            lv_address.setSelection(0);
        } else if (list == 3) {//更新三级（即区）列表
            tab_layout.removeAllTabs();
            //省
            tabProvince = tab_layout.newTab().setText(selectProvince);
            tab_layout.addTab(tabProvince);
            //市
            tabCity = tab_layout.newTab().setText(selectCity);
            tab_layout.addTab(tabCity);
            //区/县
            tabArea = tab_layout.newTab().setText("请选择");
            tab_layout.addTab(tabArea);

            tabArea.select();
            tabIndex = 2;

            areaIndex = -1;
            addressAdapter.setSelectedPos(areaIndex);
            areas.clear();
            areas.addAll(addressList);
            addressAdapter.setData(areas);
            lv_address.setSelection(0);
        } else if (list == 4) {//更新四级（即街道）列表
            tab_layout.removeAllTabs();
            //省
            tabProvince = tab_layout.newTab().setText(selectProvince);
            tab_layout.addTab(tabProvince);
            //市
            tabCity = tab_layout.newTab().setText(selectCity);
            tab_layout.addTab(tabCity);
            //区/县
            tabArea = tab_layout.newTab().setText(selectArea);
            tab_layout.addTab(tabArea);
            //街道
            tabStreet = tab_layout.newTab().setText("请选择");
            tab_layout.addTab(tabStreet);

            tabStreet.select();
            tabIndex = 3;

            streetIndex = -1;
            addressAdapter.setSelectedPos(streetIndex);
            streets.clear();
            streets.addAll(addressList);
            addressAdapter.setData(streets);
            lv_address.setSelection(0);
        }
        setTabTextSingle(tab_layout);
    }

    /**
     * 请求获取地址数据
     *
     * @param code
     * @param list
     * @param level
     */
    private void reqAddressData(String code, final int list, String level) {
        RequestEntityMap requestEntityMap = new RequestEntityMap();
        requestEntityMap.setCmd(AppPurchaseRouterConstant.cmd_address_data);
        requestEntityMap.putParameter("code", code);
        requestEntityMap.putParameter("level", level);

        request(requestEntityMap, AppPurchaseRouterConstant.cmd_address_data, new OnRequestListener<List<PopupAddressEntity>>() {
            @Override
            public List<PopupAddressEntity> jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, new TypeToken<List<PopupAddressEntity>>() {
                }.getType());
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                T.toast(failCode + "");
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                T.toast(failCode + "");
            }

            @Override
            public void onStart(Object tag) {

            }

            @Override
            public void onResponse(ResponseEntity<List<PopupAddressEntity>> response, Object tag) {
                if (response.getResponseElement() != null) {
                    updateAddressList(list, response.getResponseElement());
                }
            }
        });
    }

    /**
     * 设置tab字体单行显示
     * @param tabLayout
     */
    private void setTabTextSingle(final TabLayout tabLayout) {
        tabLayout.post(new Runnable() {

            @Override
            public void run() {
                try {
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
                        mTextView.setSingleLine();
                        tabView.invalidate();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
